<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\modules\Cruise\models\Image;
use app\modules\Cruise\models\Cruise;
use app\modules\Finance\models\FinanceImg;
use yii\web\UploadedFile;
use PhpOffice\PhpWord\PhpWord;
use dektrium\user\models\User;

class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    //public function behaviors()
    //{
	//	return [
	//		'verbs' => [
	//			'class' => VerbFilter::className(),
	//			'actions' => [
	//				'delete' => ['post'],
	//			],
	//		],
	//		'access' => [
	//			'class' => AccessControl::className(),
	//			'rules' => [
	//				['allow' => true, 'actions' => ['index','create','view','update','delete'], 'roles' => ['@']],
	//			],
	//		],
	//	];
    //}
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                     'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        //return [
        //    'error' => [
        //        'class' => 'yii\web\ErrorAction',
        //    ],
        //    'captcha' => [
        //        'class' => 'yii\captcha\CaptchaAction',
        //        'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        //    ],
        //];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        //return $this->render('index');
		if(Yii::$app->user->isGuest){
		
			return $this->redirect("/front/default");
		}else{
			return $this->redirect("/product/product");
		}
    }


	// 修改密码
	public function actionUpdatePassword(){//{{{

		if(Yii::$app->user->isGuest){
			return $this->redirect("/user/login");
		}
	
        return $this->render('password');
	
	}//}}}

    public function actionSavePassword(){

        $user = User::findOne(\yii::$app->user->id);

        $user->scenario = 'update';

        $user->password = \yii::$app->request->post('password');

        if($user->save()) {
            print_r(json_encode(['code'=>1,'msg'=>'修改成功']));
            return false;
        }else{
            print_r(json_encode(['code'=>0,'msg'=>'修改失败']));
            return false;
        }

    }

	
    public function actionLogout()
    {
        \Yii::$app->getUser()->logout();

        return $this->redirect("/user/login");
    }

    /**
     * Login action.
     *
     * @return string
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }
     */

    /**
     * Logout action.
     *
     * @return string
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
     */

    /**
     * Displays contact page.
     *
     * @return string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    /**
     * 文件上传 
     *
     */
    public function actionFileUpload(){//{{{
        $model = new Image();

        $upload = UploadedFile::getInstance($model, "path");

        $dir = sprintf("uploads/%s",date("Ym"));

        if(!is_readable($dir)) {
            is_file($dir) or mkdir($dir,0755);
        }

        $suffix = $upload->extension;

        $path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

        $ret = $upload->saveAs($path);
        if($ret){
            $params =  Yii::$app->request->get();
            $model->path = $path;
            $model->product_id = $params['product_id'];
            $model->type = $params['type'];
            $model->save();
        }
        print_r(json_encode($path));
    }//}}}


    public function actionFinance(){//{{{
        $model = new FinanceImg();

        $upload = UploadedFile::getInstance($model, "img");

        $dir = sprintf("uploads/finance/%s",date("Ym"));

        if(!is_readable($dir)) {
            is_file($dir) or mkdir($dir,0755);
        }

        $suffix = $upload->extension;

        $path = sprintf('%s/%s.%s', $dir, uniqid(true), $suffix);

        $ret = $upload->saveAs($path);
        if($ret){
            $params =  Yii::$app->request->get();
            $model->img = $path;
            $model->finance_id = $params['id'];
            $model->save();
        }
        print_r(json_encode($path));
    }//}}}



    // 二级联动城市获取
    public function actionCity(){//{{{
        //\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $cruise_id =  Yii::$app->request->get('cruise_id');

        //$res = Cruise::findOne()->select('from_city, to_city')->where(['id'=>$curise_id])->asArray()->all();
        $model = Cruise::findOne($cruise_id);

        //print_r();
        $ret = ['from_city'=>$model->from_city, 'to_city'=>$model->to_city];
        print_r(json_encode($ret));
    }//}}}



    public function actionOcr(){
        
        return $this->render('ocr');
    }


	public function actionTest(){
		
        return $this->render('test');
	}




}




